﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Nodes;
using Dogshow.Model;
using Dogshow.Win.UserControls;
using Shell;

namespace Dogshow.Win.Views
{
    public partial class IzlozbeView : ViewBase
    {
        public IzlozbeView()
        {
            InitializeComponent();
            treeList1.Click += (s, e) =>
                               {
                                   var action = (Action<int>) treeList1.FocusedNode.Tag;
                                   var idValue = treeList1.FocusedNode.GetValue("Id");
                                   if (action != null && idValue != null)
                                   {
                                       action((int)idValue);
                                   }
                               };
        }

        protected override void OnLoad(EventArgs e)
        {
            OnRefresh();
        }

        public override void OnRefresh()
        {
            var izlozbe = new List<Izlozba>
                          {
                              new Izlozba() {Id = 1, Datum = DateTime.Now, NazivIzlozbe = "CACIB Beograd 18.3.2012"},
                              new Izlozba() {Id = 2, Datum = DateTime.Now, NazivIzlozbe = "CAC Novi Sad 20.6.2012"}
                          };
            treeList1.BeginUnboundLoad();
            treeList1.Nodes.Clear();
            foreach (var izlozba in izlozbe)
            {
                var node = treeList1.AppendNode(new object[]{izlozba.NazivIzlozbe, izlozba.Datum, izlozba.Id}, null);
                node.ImageIndex = 0;

                var p = treeList1.AppendNode(new object[]{"Prijave", izlozba.Datum, izlozba.Id}, node);
                p.ImageIndex = 2;
                p.SelectImageIndex = 2;
                p.Tag = new Action<int>(OnPrijave);

                var r = treeList1.AppendNode(new object[] { "Ringovi", izlozba.Datum, izlozba.Id }, node);
                r.ImageIndex = 2;
                r.SelectImageIndex = 2;
                r.Tag = new Action<int>(OnRingovi);

            }
            treeList1.EndUnboundLoad();
        }

        private void OnRingovi(int id)
        {
            panelControl1.Controls.Clear();
            var r = new Ringovi(this, id)
            {
                Dock = DockStyle.Fill
            };
            panelControl1.Controls.Add(r);
        }

        private void OnPrijave(int id)
        {
            panelControl1.Controls.Clear();
            var p = new Prijave(this, id) {Dock = DockStyle.Fill};
            panelControl1.Controls.Add(p);
        }
    }
}
